<template>
  <div class="addsubjects-container">
    <el-form
      :model="addRuleForm"
      :rules="AddRules"
      ref="addRuleForm"
      label-width="100px"
      class="demo-addRuleForm"
    >
      <el-form-item label="活动名称" prop="subjectName">
        <el-input
          v-model="addRuleForm.subjectName"
          placeholder="请输入学科名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="是否显示" prop="isFrontDisplay">
        <el-switch
          v-model="addRuleForm.isFrontDisplay"
          active-color="#13ce66"
          inactive-color="#ff4949"
        >
        </el-switch>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('close')">取 消</el-button>
      <el-button type="primary" @click="AddDisciplines">确 定</el-button>
    </span>
  </div>
</template>

<script>
import { add as AddSubjects } from '@/api/hmmm/subjects.js'
export default {
  name: 'AddSubjects',
  components: {},
  data () {
    return {
      addRuleForm: {
        subjectName: '',
        isFrontDisplay: true
      },
      AddRules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' },
          { min: 1, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {},
  created () {},
  methods: {
    async AddDisciplines () {
      try {
        await AddSubjects({
          subjectName: this.addRuleForm.subjectName,
          isFrontDisplay: this.addRuleForm.isFrontDisplay === true ? 1 : 0
        })
        this.$message({
          message: '新增成功',
          type: 'success'
        })
        this.$emit('close')
        // 调用父组件方法刷新
        this.$emit('Refresh')
      } catch (error) {
        this.$message.error('新增失败')
        console.log(error)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.addsubjects-container {
  .dialog-footer {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
